{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Error handling "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sai\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "can only concatenate str (not \"int\") to str",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-1-d92249210e44>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"sai\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0madd\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"sai\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"sai\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
     ]
    }
   ],
   "source": [
    "print(\"sai\")\n",
    "\n",
    "add = \"sai\" + 2\n",
    "\n",
    "print(\"sai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "ZeroDivisionError",
     "evalue": "division by zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-2-e94ef31e0b2a>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdiv\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m67\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
     ]
    }
   ],
   "source": [
    "div = 67/0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Exceptions handling \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sai\n",
      "There was an error !!-  unsupported operand type(s) for +: 'int' and 'str'\n",
      "sai\n"
     ]
    }
   ],
   "source": [
    "print(\"sai\")\n",
    "\n",
    "try:\n",
    "    add = 3 + \"sai\"\n",
    "    print(add)\n",
    "except Exception as e:\n",
    "    print(\"There was an error !!- \", e)\n",
    "\n",
    "print(\"sai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There was an error in the file handling \n",
      "Error was -  not writable\n",
      "Hey guys, rate the session as of right now\n"
     ]
    }
   ],
   "source": [
    "# WAP for writing a file\n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"r\")\n",
    "\n",
    "try:\n",
    "    file.write(\"HI Guys\")\n",
    "except Exception as e:\n",
    "    print(\"There was an error in the file handling \")\n",
    "    print(\"Error was - \", e)\n",
    "finally:\n",
    "    file.close()\n",
    "    \n",
    "print(\"Hey guys, rate the session as of right now\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unit Testing !!!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Testing is a part of software development, in which your each simple module is tested. \n",
    "\n",
    "- To make sure it is enacting the same way for it was designed \n",
    "\n",
    "- and also to make sure that our code is industry standard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyLint - \n",
    "\n",
    "for what reason Pylint is been used, ?\n",
    "\n",
    "Pylint is a Python static code analysis tool which looks for programming errors, helps enforcing a coding standard, sniffs for code smells and offers simple refactoring suggestions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pylint in /opt/anaconda3/lib/python3.7/site-packages (2.4.2)\n",
      "Requirement already satisfied: isort<5,>=4.2.5 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (4.3.21)\n",
      "Requirement already satisfied: astroid<2.4,>=2.3.0 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (2.3.1)\n",
      "Requirement already satisfied: mccabe<0.7,>=0.6 in /opt/anaconda3/lib/python3.7/site-packages (from pylint) (0.6.1)\n",
      "Requirement already satisfied: lazy-object-proxy==1.4.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.2)\n",
      "Requirement already satisfied: wrapt==1.11.* in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.11.2)\n",
      "Requirement already satisfied: typed-ast<1.5,>=1.4.0; implementation_name == \"cpython\" and python_version < \"3.8\" in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.4.1)\n",
      "Requirement already satisfied: six==1.12 in /opt/anaconda3/lib/python3.7/site-packages (from astroid<2.4,>=2.3.0->pylint) (1.12.0)\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install pylint"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We will use Magic Function -- "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting treebox.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile treebox.py\n",
    "'''\n",
    "A Simple Python program for our testing purpose\n",
    "'''\n",
    "\n",
    "A = 1\n",
    "B = 2\n",
    "\n",
    "def add(num1, num2):\n",
    "    '''\n",
    "    this is a Addition function, which prints the addition of two numbers\n",
    "    '''\n",
    "    addition = num1 + num2\n",
    "    print(addition)\n",
    "\n",
    "add(A, B)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "-------------------------------------------------------------------\r\n",
      "Your code has been rated at 10.00/10 (previous run: 8.33/10, +1.67)\r\n",
      "\r\n"
     ]
    }
   ],
   "source": [
    "! Pylint treebox.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "************* Module myCode\n",
      "myCode.py:1:0: C0103: Module name \"myCode\" doesn't conform to snake_case naming style (invalid-name)\n",
      "\n",
      "-----------------------------------\n",
      "Your code has been rated at 8.33/10\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! Pylint myCode.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Unittesting "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# What is unit testing ?\n",
    "\n",
    "- unit are the smallest possible block of our program, \n",
    "\n",
    "- like functions and other stuff, \n",
    "\n",
    "- we want to test them that they are performing the same manner what we want them to do \n",
    "\n",
    "- Then we use unit testing -- \n",
    "\n",
    "\n",
    "- AGENDA - to Learn How UNIT TESTIN Works !!!\n",
    "\n",
    "\n",
    "- Step 1  - Create a new Python File - AreaOfCircle\n",
    "- Step 2  - Create a UNIT-TESTING file which can test your AreaOfCircle\n",
    "- Step 3  - \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting area_of_circle.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile area_of_circle.py\n",
    "'''\n",
    "this is a small python file which is used for area of circle\n",
    "'''\n",
    "def areaCircle(radius):\n",
    "    '''\n",
    "    this is a function for area of circle\n",
    "    '''\n",
    "    return 3.14*radius * radius\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting circle_area_testing.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile circle_area_testing.py\n",
    "\n",
    "import unittest\n",
    "import area_of_circle\n",
    "\n",
    "class CircleTesting(unittest.TestCase):\n",
    "    def testRadius(self):  # TestCase 1\n",
    "        temp_radius = 4\n",
    "        result = area_of_circle.areaCircle(temp_radius)\n",
    "        #Important line\n",
    "        self.assertAlmostEqual(result,50.24)\n",
    "        # THe above line checks your result from function is equal to \n",
    "        # the manully given result, \n",
    "        # if yes then it says test passed \n",
    "        # if no then it says test failed\n",
    "        \n",
    "    def testRadiusTwo(self): # TestCase 2\n",
    "        temp_radius = 101\n",
    "        result = area_of_circle.areaCircle(temp_radius)\n",
    "        self.assertAlmostEqual(result,32031.14)\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    unittest.main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\r\n",
      "----------------------------------------------------------------------\r\n",
      "Ran 2 tests in 0.000s\r\n",
      "\r\n",
      "OK\r\n"
     ]
    }
   ],
   "source": [
    "! python circle_area_testing.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50.24"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import area_of_circle\n",
    "\n",
    "area_of_circle.areaCircle(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
